package com.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.order.entity.ResOrderStage;
import com.bhudy.enums.Relation;
import com.sys.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bhudy.base.QueryFilter;
import com.order.service.ResOrderStageService;

import java.util.Map;

/**
 * @Author 卞洪业
 * @Description ResOrderStageController
 * @Date Wed Feb 19 17:04:56 CST 2020
 */
@RestController
public class ResOrderStageController {

    @Autowired
    private ResOrderStageService resOrderStageService;

    /**
     * 获取ResOrderStage表的数据
     *
     * @param page  页数{1}
     * @param limit 显示数量{10}
     * @return ResOrderStage的list
     */
    @GetMapping(value = "/api/v1/resOrderStages")
    public Object getAll(Integer page, Integer limit) {
        QueryFilter queryFilter = new QueryFilter().and("status", Relation.NOT_EQUAL, 1);
        if (page != null && limit != null) queryFilter.limit(--page, limit);
        return RequestUtils.printMapJson(0, resOrderStageService.findList(queryFilter));
    }

    @GetMapping(value = "/api/v1/resOrderStage")
    public Object get(Long resOrderStageId) {
        return RequestUtils.printMapJson(0, resOrderStageService.get(resOrderStageId));
    }

    @PostMapping(value = "/api/v1/resOrderStage")
    public Object insert(@RequestBody String json) {
        if (json == null) throw new SysException(SysExceptionCode.CODE_40003);
        Map<String, Object> resOrderStageMap = JSONObject.parseObject(json, Map.class);

        return RequestUtils.printMapJson(0, resOrderStageService.insert(resOrderStageMap));
    }

    @PutMapping(value = "/api/v1/resOrderStage")
    public Object update(@RequestBody String json) {
        return RequestUtils.printMapJson(0, resOrderStageService.update(JSONObject.parseObject(json, ResOrderStage.class)));
    }

    @DeleteMapping(value = "/api/v1/resOrderStages")
    public Object delete(@RequestBody String json) {
        return RequestUtils.printMapJson(0, resOrderStageService.delete(JSONObject.parseObject(json, Long[].class)));
    }

}
